package com.ruoyi.safe.controller.web;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.safe.domain.BizEducate;
import com.ruoyi.safe.domain.dto.EntityIdsDTO;
import com.ruoyi.safe.domain.vo.BizEducateVO;
import com.ruoyi.safe.service.IBizEducateService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班前教育----->张鹏
 *
 * @author 张鹏
 * @date 2023-12-21
 */

@RestController
@RequestMapping("/educate/educate")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class BizEducateController extends BaseController {

    private final IBizEducateService bizEducateService;

    /**
     * 查询班前教育列表
     */
    @PreAuthorize("@ss.hasPermi('educate:educate:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizEducate bizEducate) {
        PageUtils.orderBy("educate_time desc");
        List<BizEducateVO> bizEducateVOS = bizEducateService.selectBizEducateList(bizEducate);
        return getDataTable(bizEducateVOS);
    }

    /**
     * 获取班前教育详细信息
     */
    @PreAuthorize("@ss.hasPermi('educate:educate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizEducateService.selectBizEducateById(id));
    }

    /**
     * 新增班前教育
     */
    @PreAuthorize("@ss.hasPermi('educate:educate:add')")
    @Log(title = "班前教育", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizEducate bizEducate) {
        return toAjax(bizEducateService.insertBizEducate(bizEducate));
    }

    /**
     * 修改班前教育
     */
    @PreAuthorize("@ss.hasPermi('educate:educate:edit')")
    @Log(title = "班前教育", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizEducate bizEducate) {
        return toAjax(bizEducateService.updateBizEducate(bizEducate));
    }

    /**
     * 删除班前教育
     */
    @PreAuthorize("@ss.hasPermi('educate:educate:remove')")
    @Log(title = "班前教育", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(bizEducateService.deleteBizEducateById(id));
    }

    /**
     * 批量删除班前教育
     */
    @PreAuthorize("@ss.hasPermi('educate:educate:remove')")
    @Log(title = "班前教育", businessType = BusinessType.DELETE)
    @PostMapping("/deleteByIds")
    public AjaxResult deleteByIds(@RequestBody EntityIdsDTO entityIdsDTO) {
        if(entityIdsDTO.getIds().length > 0){
            return toAjax(bizEducateService.deleteBizEducateByIds(entityIdsDTO.getIds()));
        }else {
            throw new ServiceException("id集合不能为空");
        }
    }
}
